package org.leviatan.dataharbour.gui.swingcomponents.views;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;

import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTable;

import org.leviatan.dataharbour.core.model.HarbourRequest;
import org.leviatan.dataharbour.gui.RuntimeContext;
import org.leviatan.dataharbour.gui.swingmodels.TableMdlDataUnits;

public class HarbourRequestDetailView extends JPanel {

	private HarbourRequest theModel = null;
	
	
	
	private static final long serialVersionUID = -5351982764652328621L;

	private JLabel lblActorId = null;
	private JLabel lblActorIdTxt = null;
	
	private JLabel lblTaskId = null;
	private JLabel lblTaskIdTxt = null;
	
	private JLabel lblTaskName = null;
	private JLabel lblTaskNameTxt = null;
	
	private JLabel lblId = null;
	private JLabel lblIdTxt = null;
	
	private JLabel lblSenderId = null;
	private JLabel lblSenderIdTxt = null;
	
	private JLabel lblProcessDefId = null;
	private JLabel lblProcessDefIdTxt = null;
	
	private JLabel lblProcessDefName = null;
	private JLabel lblProcessDefNameTxt = null;
	
	private JLabel lblProcessDefVersion = null;
	private JLabel lblProcessDefVersionTxt = null;
	
	private JLabel lblProcessInstanceId = null;
	private JLabel lblProcessInstanceIdTxt = null;
	
	
	private JLabel lblTitle = null;
	

	/**
	 * This is the default constructor
	 */
	public HarbourRequestDetailView() {
		super();
		initialize();
		RuntimeContext.harbouRequestDetailView = this;
		RuntimeContext.dataUnitsTable = this.dataUnitsTable;
	}

	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		
		GridBagConstraints lblTitleConstr = new GridBagConstraints();
		
		GridBagConstraints lblSenderIdTxtConstr = new GridBagConstraints();
		GridBagConstraints lblSenderIdConstr = new GridBagConstraints();
		
		GridBagConstraints lblIdConstr = new GridBagConstraints();
		GridBagConstraints lblIdTxtConstr = new GridBagConstraints();
		
		GridBagConstraints lblActorIdConstr = new GridBagConstraints();
		GridBagConstraints lblActorIdTxtConstr = new GridBagConstraints();
		
		GridBagConstraints lblTaskIdConstr = new GridBagConstraints();
		GridBagConstraints lblTaskIdTxtConstr = new GridBagConstraints();
		GridBagConstraints lblTaskNameConstr = new GridBagConstraints();
		GridBagConstraints lblTaskNameTxtConstr = new GridBagConstraints();
		
		GridBagConstraints lblProcessDefIdConstr = new GridBagConstraints();
		GridBagConstraints lblProcessDefIdTxtConstr = new GridBagConstraints();
		GridBagConstraints lblProcessDefNameConstr = new GridBagConstraints();
		GridBagConstraints lblProcessDefNameTxtConstr = new GridBagConstraints();
		GridBagConstraints lblProcessDefVersionConstr = new GridBagConstraints();
		GridBagConstraints lblProcessDefVersionTxtConstr = new GridBagConstraints();
		
		
		GridBagConstraints lblProcessInstanceIdConstr = new GridBagConstraints();
		GridBagConstraints lblProcessInstanceIdTxtConstr = new GridBagConstraints();
		
		// title
		///////////////////////////////////////////////////////////////////////
		lblTitleConstr.fill = GridBagConstraints.BOTH;
		lblTitleConstr.anchor = GridBagConstraints.NORTHWEST;
		lblTitleConstr.gridheight = 1;
		lblTitleConstr.gridwidth = 1;
		lblTitleConstr.gridx = 0;
		lblTitleConstr.gridy = 0;
		lblTitleConstr.insets = new Insets(10,5,20,5);
		
		
		// sender id
		///////////////////////////////////////////////////////////////////////
		lblSenderIdConstr.fill = GridBagConstraints.BOTH;
		lblSenderIdConstr.gridx = 0;
		lblSenderIdConstr.gridy = 1;
		lblSenderIdConstr.insets = new Insets(2, 5, 2, 5);
		
		lblSenderIdTxtConstr.fill = GridBagConstraints.BOTH;
		lblSenderIdTxtConstr.gridx = 1;
		lblSenderIdTxtConstr.gridy = 1;
		lblSenderIdTxtConstr.insets = new Insets(2, 5, 2, 5);
		
		// id
		///////////////////////////////////////////////////////////////////////
		lblIdConstr.fill = GridBagConstraints.BOTH;
		lblIdConstr.gridx = 0;
		lblIdConstr.gridy = 2;
		lblIdConstr.insets = new Insets(2, 5, 2, 5);
		
		lblIdTxtConstr.fill = GridBagConstraints.BOTH;
		lblIdTxtConstr.gridx = 1;
		lblIdTxtConstr.gridy = 2;
		lblIdTxtConstr.insets = new Insets(2, 5, 2, 5);
		
		// actor
		///////////////////////////////////////////////////////////////////////
		lblActorIdConstr.fill = GridBagConstraints.BOTH;
		lblActorIdConstr.gridx = 0;
		lblActorIdConstr.gridy = 3;
		lblActorIdConstr.insets = new Insets(2, 5, 2, 5);
		
		lblActorIdTxtConstr.fill = GridBagConstraints.BOTH;
		lblActorIdTxtConstr.gridx = 1;
		lblActorIdTxtConstr.gridy = 3;
		lblActorIdTxtConstr.insets = new Insets(2, 5, 2, 5);
		
		// task
		///////////////////////////////////////////////////////////////////////
		lblTaskIdConstr.fill = GridBagConstraints.BOTH;
		lblTaskIdConstr.gridx = 0;
		lblTaskIdConstr.gridy = 4;
		lblTaskIdConstr.insets = new Insets(2, 5, 2, 5);
		
		lblTaskIdTxtConstr.fill = GridBagConstraints.BOTH;
		lblTaskIdTxtConstr.gridx = 1;
		lblTaskIdTxtConstr.gridy = 4;
		lblTaskIdTxtConstr.insets = new Insets(2, 5, 2, 5);
		
		lblTaskNameConstr.fill = GridBagConstraints.BOTH;
		lblTaskNameConstr.gridx = 0;
		lblTaskNameConstr.gridy = 5;
		lblTaskNameConstr.insets = new Insets(2, 5, 2, 5);
		
		lblTaskNameTxtConstr.fill = GridBagConstraints.BOTH;
		lblTaskNameTxtConstr.gridx = 1;
		lblTaskNameTxtConstr.gridy = 5;
		lblTaskNameTxtConstr.insets = new Insets(2, 5, 2, 5);
		
		// process definition
		///////////////////////////////////////////////////////////////////////
		lblProcessDefIdConstr.fill = GridBagConstraints.BOTH;
		lblProcessDefIdConstr.gridx = 0;
		lblProcessDefIdConstr.gridy = 6;
		lblProcessDefIdConstr.insets = new Insets(2, 5, 2, 5);
		
		lblProcessDefIdTxtConstr.fill = GridBagConstraints.BOTH;
		lblProcessDefIdTxtConstr.gridx = 1;
		lblProcessDefIdTxtConstr.gridy = 6;
		lblProcessDefIdTxtConstr.insets = new Insets(2, 5, 2, 5);
		
		lblProcessDefNameConstr.fill = GridBagConstraints.BOTH;
		lblProcessDefNameConstr.gridx = 0;
		lblProcessDefNameConstr.gridy = 7;
		lblProcessDefNameConstr.insets = new Insets(2, 5, 2, 5);
		
		lblProcessDefNameTxtConstr.fill = GridBagConstraints.BOTH;
		lblProcessDefNameTxtConstr.gridx = 1;
		lblProcessDefNameTxtConstr.gridy = 7;
		lblProcessDefNameTxtConstr.insets = new Insets(2, 5, 2, 5);
		
		lblProcessDefVersionConstr.fill = GridBagConstraints.BOTH;
		lblProcessDefVersionConstr.gridx = 0;
		lblProcessDefVersionConstr.gridy = 8;
		lblProcessDefVersionConstr.insets = new Insets(2, 5, 2, 5);
		
		lblProcessDefVersionTxtConstr.fill = GridBagConstraints.BOTH;
		lblProcessDefVersionTxtConstr.gridx = 1;
		lblProcessDefVersionTxtConstr.gridy = 8;
		lblProcessDefVersionTxtConstr.insets = new Insets(2, 5, 2, 5);
		
		// process instance
		///////////////////////////////////////////////////////////////////////
		lblProcessInstanceIdConstr.fill = GridBagConstraints.BOTH;
		lblProcessInstanceIdConstr.gridx = 0;
		lblProcessInstanceIdConstr.gridy = 9;
		lblProcessInstanceIdConstr.insets = new Insets(2, 5, 2, 5);
		
		lblProcessInstanceIdTxtConstr.fill = GridBagConstraints.BOTH;
		lblProcessInstanceIdTxtConstr.gridx = 1;
		lblProcessInstanceIdTxtConstr.gridy = 9;
		lblProcessInstanceIdTxtConstr.insets = new Insets(2, 5, 2, 5);
		
		// data units
		///////////////////////////////////////////////////////////////////////
		
		
		lblTitle = new JLabel();
		lblTitle.setText("Harbour request details");
		
		lblSenderId = new JLabel("sender id:");
		lblSenderIdTxt = new JLabel();
		
		lblId = new JLabel("id:");
		lblIdTxt = new JLabel();
		
		lblActorId = new JLabel("actor id:");
		lblActorIdTxt = new JLabel();
		
		lblTaskId = new JLabel("task id:");
		lblTaskIdTxt = new JLabel();
		
		lblTaskName = new JLabel("task name:");
		lblTaskNameTxt = new JLabel();
		
		lblProcessDefId = new JLabel("process definition id:");
		lblProcessDefIdTxt = new JLabel();
		
		lblProcessDefName = new JLabel("process name:");
		lblProcessDefNameTxt = new JLabel();
		
		lblProcessDefVersion = new JLabel("process version:");
		lblProcessDefVersionTxt = new JLabel();
		
		lblProcessInstanceId = new JLabel("process instance id:");
		lblProcessInstanceIdTxt = new JLabel();
		
		
		
		this.setSize(314, 400);
		this.setLayout(new GridBagLayout());
		
		
		this.add(lblTitle, lblTitleConstr);
		
		this.add(lblSenderId, lblSenderIdConstr);
		this.add(lblSenderIdTxt, lblSenderIdTxtConstr);
		
		this.add(lblId, lblIdConstr);
		this.add(lblIdTxt, lblIdTxtConstr);
		
		this.add(lblTaskId, lblTaskIdConstr);
		this.add(lblTaskIdTxt, lblTaskIdTxtConstr);
		this.add(lblTaskName, lblTaskNameConstr);
		this.add(lblTaskNameTxt, lblTaskNameTxtConstr);
		
		this.add(lblActorId, lblActorIdConstr);
		this.add(lblActorIdTxt, lblActorIdTxtConstr);
		
		this.add(lblProcessDefId, lblProcessDefIdConstr);
		this.add(lblProcessDefIdTxt, lblProcessDefIdTxtConstr);
		this.add(lblProcessDefName, lblProcessDefNameConstr);
		this.add(lblProcessDefNameTxt, lblProcessDefNameTxtConstr);
		this.add(lblProcessDefVersion, lblProcessDefVersionConstr);
		this.add(lblProcessDefVersionTxt, lblProcessDefVersionTxtConstr);
		
		this.add(lblProcessInstanceId, lblProcessInstanceIdConstr);
		this.add(lblProcessInstanceIdTxt, lblProcessInstanceIdTxtConstr);
	
	
		GridBagConstraints top = new GridBagConstraints();
		GridBagConstraints tblHeader = new GridBagConstraints();
		GridBagConstraints table = new GridBagConstraints();
		
		top.anchor = GridBagConstraints.NORTHWEST;
		top.gridx = 0;
		top.gridy = 0;
		top.insets = new Insets(5, 5, 5, 5);
		//top.weightx = 1.0;
		//top.weighty = 1.0;
		
		tblHeader.anchor = GridBagConstraints.NORTHWEST;
		tblHeader.gridx = 0;
		tblHeader.gridy = 10;
		tblHeader.gridwidth = 4;
		//header.weightx = 1.0;
		//header.weighty = 1.0;
		tblHeader.fill = GridBagConstraints.BOTH;
		tblHeader.insets = new Insets(10, 5, 0, 5);
		
		
		table.anchor = GridBagConstraints.NORTHWEST;
		table.gridx = 0;
		table.gridy = 11;
		table.gridwidth = 4;
		table.weightx = 1.0;
		table.weighty = 1.0;
		table.fill = GridBagConstraints.BOTH;
		table.insets = new Insets(5, 5, 5, 5);
		
		this.add(dataUnitsTable.getTableHeader(), tblHeader);
		this.add(dataUnitsTable, table);
		
		///////////////////////////////////////////////////////////////////////
		
		/*
		this.butDiscard.addMouseListener(new MouseAdapter() {
			public void mouseClicked(MouseEvent e) {
				System.out.println("Discard clicked");
				Field fldModel = RuntimeContext.definatorService.getFieldModel(
						Long.parseLong(lblIdTxt.getText()));
				setModel(fldModel);
			}
		});
		*/
	}
	
	public void setModel(HarbourRequest reqMdl) {
		if (reqMdl == null)
			return;
		
		this.theModel = reqMdl;
		this.lblIdTxt.setText(reqMdl.getId());
		this.lblSenderIdTxt.setText(reqMdl.getSenderId());
		this.lblActorIdTxt.setText(reqMdl.getActorId());
		this.lblTaskIdTxt.setText(reqMdl.getTaskId());
		this.lblTaskNameTxt.setText(reqMdl.getTaskName());
		this.lblProcessDefIdTxt.setText(reqMdl.getProcessId());
		this.lblProcessDefNameTxt.setText(reqMdl.getProcessName());
		this.lblProcessDefVersionTxt.setText(String.valueOf(reqMdl.getProcessVersion()));
		this.lblProcessInstanceIdTxt.setText(reqMdl.getProcessInstanceId());
		
		// update table
		TableMdlDataUnits tblData = (TableMdlDataUnits) this.dataUnitsTable.getModel();
		tblData.setData(reqMdl.getDataEntityMap());
		tblData.fireTableDataChanged();
		
		//this.txtFldTaskName.setText(reqMdl.getName());
		//this.lblProcessId.setText(String.valueOf(reqMdl.getLength()));
		// TODO
		
		//this.lblAuthorTxt.setText(" - ");
		//this.cmbType.setSelectedItem(fldMdl.getType());
		
	}
	
	/* DATA UNITS VIEW */
	///////////////////////////////////////////////////////////////////////////
	private JTable dataUnitsTable = new JTable(new TableMdlDataUnits());
	
	
		
}
